import { fetchZhiChuType, fetchShouRuType, addZhiChuCategory, addShouRuCategory, updateZhiChuCategory, updateShouRuCategory } from '@/apis/index'
import { SHOURU_ICON_NAME_MAP, ZHICHU_ICON_NAME_MAP } from '@/utils/map'
export default {
  namespaced: true,
  state: () => {
    return {
      zhiChuType: [],
      shouRuType: []
    }
  },
  mutations: {
    setZXhiChuType(state, { zhiChuType = [] }) {
      state.zhiChuType = zhiChuType
    },
    setShouRuType(state, { shouRuType = [] }) {
      state.shouRuType = shouRuType
    }
  },
  actions: {
    async asyncFetchZhiChuType(context) {
      const r = await fetchZhiChuType()
      r.forEach(item => item.iconName = ZHICHU_ICON_NAME_MAP[item.id] || 'icon-morennv-1')
      r.sort((a, b) => a.sort - b.sort)
      context.commit('setZXhiChuType', { zhiChuType: r })

    },
    async asyncFetchShouRuType(context) {
      const r = await fetchShouRuType()
      r.forEach(item => item.iconName = SHOURU_ICON_NAME_MAP[item.id] || 'icon-morennan-1')
      r.sort((a, b) => a.sort - b.sort)
      context.commit('setShouRuType', { shouRuType: r })

    },
    async asyncAddZhiChuCategory(contecxt, paylod) {
      await addZhiChuCategory(paylod)
      contecxt.dispatch('asyncFetchZhiChuType') //重新拉去数据
    },
    async asyncAddShouRuCategory(contecxt, paylod) {
      await addShouRuCategory(paylod)
      contecxt.dispatch('asyncFetchShouRuType') //重新拉去数据
    },

    async asyncUpdateZhiChuCategory(context, payload={}) {
      await updateZhiChuCategory(payload.id, payload.data)
      context.dispatch('asyncFetchZhiChuType')
    },
    async asyncUpdateShouRuCategory(context, payload = {}) {
      await updateShouRuCategory(payload.id, payload.data)
      context.dispatch('asyncFetchShouRuType')
    }
  }
}

/**
 * 
import { createNamespacedHelpers } from 'vuex'
const { mapMutations: CategoryMapMutations } = createNamespacedHelpers('Category')
 */